package com.xms.security.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xms.core.model.PagedList;
import com.xms.security.entity.Roles;
import com.xms.security.service.role.IRolesFinderService;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * <p>
 * RolesFinderController
 * </p>
 *
 * @author migo
 * @since 2021/10/8
 */
@RestController
@RequestMapping("/{org}/api/security/role")
public class RolesFinderController extends ApiBaseController {
    private IRolesFinderService _rolesFinderService;
    @Autowired
    public void RolesFinderController(IRolesFinderService rolesFinderService) {
        _rolesFinderService = rolesFinderService;
    }

    @ApiOperation("查询角色")
    @GetMapping("list")
    public JsonResultObject list(@RequestParam(value = "statecode", required = false) Boolean statecode,
                                 @RequestParam(value="getall", required = false) Boolean getall,
                                 @RequestParam(value="page", required = false) Integer page,
                                 @RequestParam(value="pagesize", required = false) Integer pagesize,
                                 @RequestParam(value="sortby", required = false) String sortby,
                                 @RequestParam(value="sortdirection", required = false) String sortdirection) {
        LambdaQueryWrapper<Roles> lambdaQueryWrapper = getSortedQueryWrapper(sortby, sortdirection);
        if (statecode != null) {
            lambdaQueryWrapper.eq(Roles::getStatecode, statecode);
        }
        if (getall != null && getall) {
            return JOk(_rolesFinderService.list(lambdaQueryWrapper));
        }
        else if(page == null || pagesize == null){
            return JNotSpecifiedParams("page","pagesize");
        }
        page = (page <= 0 ? 1 : page);
        pagesize = (pagesize <= 0 ? 10 : pagesize);
        IPage<Roles> datas = _rolesFinderService.page(new Page<>(page, pagesize), lambdaQueryWrapper);
        PagedList<Roles> result = new PagedList<>(datas.getRecords(), page, pagesize, datas.getTotal(), datas.getPages());

        return JOk(result);
    }

    @ApiOperation("根据ID查询一个角色")
    @GetMapping("getbyid/{id}")
    public JsonResultObject getById(@PathVariable(value = "id", required = true) UUID id){
        return JOk(_rolesFinderService.getById(id));
    }
}
